(function (window, undefined) {

    var Carousel = function (ele, obj) {

        this.ele = ele;//ul容器
        this.availWidth = this.ele.outerWidth();//ul可见宽度
        this.cards_list = this.ele.children();//卡片
        this.cards_list_width = this.cards_list.eq(0).outerWidth();//单张卡片宽度
        this.cards_num = this.cards_list.length;//卡片数量
        this.offset = obj.offset || this.availWidth / (this.cards_num - 1);//获取公共偏移量，计算translate值时需用到

        this.opacity = obj.opacity || 1;//透明度
        this.transition = obj.transition || "500ms";//过渡动画时间
        this.switch_btn = obj.switch_btn;//切换按钮
        this.transform_origin = obj["transform-origin"] || "center center";//transform原点
        this.isClickCard = obj.isClickCard || false;//是否点击卡片切换
        this.isAuto = obj.isAuto || false;
        this.interval = obj.interval || 5000;

        this.n = 0;
        this.mediant = Math.floor(this.cards_num / 2);
        this.cards_list_arr = [].slice.call(this.cards_list);//将卡片对象存到数组中
        this.timer = null;

        this.init(obj);
        this.switchEffect();
        this.cardsClick();
        this.switchBtnClick();
        this.autoMove();

    };

    Carousel.prototype = {

        constructor: Carousel,

        init: function (obj) {

            var $Carousel = this;
            $Carousel.scale = obj.scale || [1, 1];
            if ($Carousel.scale.length == 1) { $Carousel.scale.push($Carousel.scale[0]) };

            if (this.isAuto) {
                $Carousel.cards_list.on("mouseover", function () {
                    clearInterval($Carousel.timer);
                });
                $Carousel.cards_list.on("mouseout", function () {
                    $Carousel.autoMove();
                });
            };

        },

        switchEffect: function () {

            var $Carousel = this;

            var calcNum = Math.abs($Carousel.n - $Carousel.mediant);

            if ($Carousel.n == $Carousel.mediant) {
                return;
            } else if ($Carousel.n > $Carousel.mediant) {
                $Carousel.cards_list_arr = $Carousel.cards_list_arr.concat($Carousel.cards_list_arr.splice(0, calcNum));
            } else {
                $Carousel.cards_list_arr = $Carousel.cards_list_arr.splice($Carousel.cards_num - calcNum).concat($Carousel.cards_list_arr);
            };

            $($Carousel.cards_list_arr).each(function (index, element) {

                var offset_index = index - $Carousel.mediant,
                    offset_index_abs = Math.abs(offset_index),
                    reset = $Carousel.mediant - Math.abs(offset_index);

                $(element).css({

                    "position": "absolute",
                    "left": "50%",
                    "transform": "translate(" + (- $Carousel.cards_list_width / 2 + $Carousel.offset * offset_index) + "px) " +
                        "scale(" + Math.pow($Carousel.scale[0], offset_index_abs) + "," + Math.pow($Carousel.scale[1], offset_index_abs) + ")",
                    "transform-origin": $Carousel.transform_origin,
                    "opacity": Math.pow($Carousel.opacity, offset_index_abs),
                    "transition": $Carousel.transition,
                    "z-index": reset,

                });

                $(element).attr("n", index);

            });
        },

        cardsClick: function () {

            if (this.isClickCard) {

                var $Carousel = this;

                $($Carousel.cards_list_arr).on("click", function () {

                    $Carousel.n = $(this).attr("n");

                    $Carousel.switchEffect();

                });

            };

        },

        prev: function () {

            var $Carousel = this;

            $Carousel.n = $Carousel.mediant - 1;

            $Carousel.n < 0 && ($Carousel.n = $Carousel.cards_num - 1);

            $Carousel.switchEffect();

        },

        next: function () {

            var $Carousel = this;

            $Carousel.n = $Carousel.mediant + 1;

            $Carousel.n > $Carousel.cards_num - 1 && ($Carousel.n = 0);

            $Carousel.switchEffect();

        },

        switchBtnClick: function () {

            var $Carousel = this;

            if ($Carousel.switch_btn) {

                $Carousel.switch_btn.eq(0).on("click", function () {

                    $Carousel.prev();

                    if ($Carousel.isAuto) {
                        clearInterval($Carousel.timer);
                        $Carousel.cards_list.eq($Carousel.n).one("transitionend", function () {
                            $Carousel.autoMove();
                        });

                    };

                });

                $Carousel.switch_btn.eq(1).on("click", function () {

                    $Carousel.next();
                    if ($Carousel.isAuto) {
                        clearInterval($Carousel.timer);
                        $Carousel.cards_list.eq($Carousel.n).one("transitionend", function () {
                            $Carousel.autoMove();
                        });
                    };
                });

            };

        },

        autoMove: function () {

            var $Carousel = this;

            if ($Carousel.isAuto) {
                clearInterval($Carousel.timer);
                $Carousel.timer = setInterval(function () {
                    $Carousel.next();
                }, $Carousel.interval);
            };

        },

    };

    window.Carousel = Carousel;

}(window));